<template>
  <div>
    <van-tabs v-model="active">
      <van-tab
        :title="item.name"
        v-for="item in brotherCategory"
        :key="item.id"
      >
      </van-tab>
      <div class="box">
        <h2>{{ currentCategory.name }}</h2>
        <p>{{ currentCategory.front_desc }}</p>
      </div>
      <Products :goodsList="goodsList" />
    </van-tabs>

    {{ $route.query.id }}
  </div>
</template>

<script>
import { goodsCategory, GetSearchData } from "@/request/api";
import Products from "@/components/Products";
export default {
  data() {
    return {
      active: "",
      brotherCategory: [],
      currentCategory: {},
      goodsList: [],
    };
  },
  created() {
    goodsCategory({
      id: this.$route.query.id,
    }).then((res) => {
      let { brotherCategory, currentCategory } = res.data;
      this.brotherCategory = brotherCategory;
      this.currentCategory = currentCategory;
      // console.log(this.currentCategory);
      //遍历顶部导航数组，判断数组中哪一个ID和当前ID相同 则active（控制顶部导航到什么地方的值）就等于当前索引。
     this.brotherCategory.map((item, index) => {
        if (item.id == this.$route.query.id) {
          this.active = index;
        }
      });
    });

    GetSearchData().then((res) => {
      //   console.log(res);
      let { goodsList } = res.data;
      this.goodsList = goodsList;
    });
  },
  watch: {
    active(val) {
      GetSearchData({
        categoryId: this.brotherCategory[val].id,
        page: 1,
        size: 1000,
      }).then((res) => {
        // console.log(res);
        let { goodsList } = res.data;
        this.goodsList = goodsList;
      });
      goodsCategory({
      id: this.brotherCategory[val].id,
    }).then((res) => {
      // console.log(this.brotherCategory[val].id);
      let { currentCategory } = res.data;
      this.currentCategory = currentCategory;
    });
    },
  },
  components: {
    Products,
  },
};
</script>
 
<style lang = "less" scoped>
.box {
  background-color: #efefef;
  text-align: center;
  h2 {
    font-size: 18px;
    height: 50px;
    line-height: 50px;
  }
  p {
    height: 30px;
    line-height: 30px;
    color: #666;
  }
}
</style>